// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/expression/signature_parser/parse_util.h>
#include <string>
#include <pollux/type/type.h>

namespace kumo::pollux::exec {

TypeSignaturePtr inferTypeWithSpaces(
    const std::vector<std::string>& words,
    bool canHaveFieldName) {
  POLLUX_CHECK_GE(words.size(), 2);
  const auto& fieldName = words[0];
  const auto allWords = melon::join(" ", words);
  if (hasType(allWords) || !canHaveFieldName) {
    return std::make_shared<exec::TypeSignature>(
        exec::TypeSignature(allWords, {}));
  }
  return std::make_shared<exec::TypeSignature>(exec::TypeSignature(
      allWords.data() + fieldName.size() + 1, {}, fieldName));
}

std::unordered_set<std::string> splitNames(const std::string& names) {
  // Parse, lower case and trim it.
  std::vector<melon::StringPiece> nameList;
  melon::split(',', names, nameList);
  std::unordered_set<std::string> nameSet;

  for (const auto& it : nameList) {
    auto str = melon::trimWhitespace(it).toString();
    melon::toLowerAscii(str);
    nameSet.insert(str);
  }
  return nameSet;
}

} // namespace kumo::pollux::exec
